/**
 * 
 */
package hr.fer.zemris.rznu.model.user;

import hr.fer.zemris.rznu.model.authorization.UserInRole;
import hr.fer.zemris.rznu.model.betting.BettingSlip;
import hr.fer.zemris.rznu.model.dto.user.UserDTO;
import hr.fer.zemris.rznu.model.factories.ConverterFromDTOFactory;

import java.util.Set;
import javax.persistence.NamedQuery;

import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

/**
 * @author Dajan
 *
 */
@Entity
@Table(name="USER")
@NamedQueries({
		@NamedQuery(name = "userByUserName", query ="SELECT u FROM User u WHERE u.username like :username"),
		@NamedQuery(name = "allUsers", query = "SELECT u FROM User u")
})
public class User {

	@Id
	@GeneratedValue
	private Integer id;

	/**
	 * Personal user info.
	 */
	@Embedded
	private PersonalUserInfo personalUserInfo;

	/**
	 * Username.
	 */
	@Column(name = "username", length = 50, unique = true, nullable = true)
	private String username;
	/**
	 * Password. Password is encrypted with SHA1 encryption. Encrypted password
	 * is always 20 characters long.
	 */
	@Column(name = "password", length = 40, nullable = true)
	private String password;

	/**
	 * User roles.
	 */
	@OneToMany(mappedBy="user")
	private Set<UserInRole> usersInRole;
	
	@OneToMany(mappedBy="user")
	private Set<BettingSlip> bettingSlips;
	
	public User(){
		
	}
	
	public User(UserDTO userDTO){
		
		this.bettingSlips = ConverterFromDTOFactory.getBettingSlips(userDTO.getBettingSlips());
		this.password = userDTO.getPassword();
		this.personalUserInfo = new PersonalUserInfo(userDTO.getPersonalUserInfoDTO());
		this.username = userDTO.getUsername();
		this.usersInRole = ConverterFromDTOFactory.getUsersInRole(userDTO.getUsersInRole());
		
		
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public PersonalUserInfo getPersonalUserInfo() {
		return personalUserInfo;
	}

	public void setPersonalUserInfo(PersonalUserInfo personalUserInfo) {
		this.personalUserInfo = personalUserInfo;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<UserInRole> getUsersInRole() {
		return usersInRole;
	}

	public void setUsersInRole(Set<UserInRole> usersInRole) {
		this.usersInRole = usersInRole;
	}

	public Set<BettingSlip> getBettingSlips() {
		return bettingSlips;
	}

	public void setBettingSlips(Set<BettingSlip> bettingSlips) {
		this.bettingSlips = bettingSlips;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.username;
	}
	
}
